package cn.icyzx.controller;

import cn.icyzx.models.User;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.icyzx.service.FindPswService;
import cn.icyzx.service.UserService;
import cn.icyzx.util.StaticConstUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author zk
 * @Date 2017/3/19.
 * @Email 2117251154@qq.com
 */
@Controller
@RequestMapping("/setting")
public class PersonalSetProcess {

    private static Logger logger = Logger.getLogger(PersonalSetProcess.class);

    @Autowired
    private UserService userService;

    @Autowired
    FindPswService findPswService;

    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public ModelAndView SetProfile(ModelAndView modelAndView) {
        modelAndView = new ModelAndView("personal-set", StaticConstUtil.p_profile, true);
        return modelAndView;
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public ModelAndView setPassword(ModelAndView modelAndView) {
        modelAndView = new ModelAndView("personal-set-password", StaticConstUtil.p_password, true);
        return modelAndView;
    }

    @RequestMapping(value = "/avatar", method = RequestMethod.GET)
    public ModelAndView setAvatar(ModelAndView modelAndView) {
        modelAndView = new ModelAndView("personal-set-avatar", StaticConstUtil.p_avatar, true);
        return modelAndView;
    }

    @RequestMapping(value = "/college", method = RequestMethod.GET)
    public ModelAndView setCollege(ModelAndView modelAndView) {
        modelAndView = new ModelAndView("personal-set-college", StaticConstUtil.p_college, true);
        return modelAndView;
    }

    /*** 修改基本信息页，包括sign username favorite
     *
     * @param sign
     * @param username
     * @param favorite
     * @param session
     * @return model
     */
    @RequestMapping(value = "/modify/profile", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    String updateBaseInfo(@RequestParam("sign") String sign, @RequestParam("username") String username,
                          @RequestParam("favorite") String favorite, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (!username.equals(""))
            user.setUsername(username);
        if (!sign.equals(""))
            user.setSign(sign);
        if (!favorite.equals(""))
            user.setFavorite(favorite);

        userService.updateBaseInfo(user);
        userService.updateSessionUser(session);
        return StaticConstUtil.STATUS_SUCCESS;
    }

    /**
     * 更改密码操作
     *
     * @param oldPassword
     * @param password
     * @param request
     * @return
     */
    @RequestMapping(value = "/modify/password", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    String updatePassword(@RequestParam("oldPassword") String oldPassword,
                          @RequestParam("password") String password, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (!oldPassword.equals(user.getPassword()))
            return "原密码错误";
        else {
            findPswService.modifyPsw(password, user.getUid());
            userService.updateSessionUser(session);
            return StaticConstUtil.STATUS_SUCCESS;
        }
    }

    /**
     * 更改头像的操作
     *
     * @param avatarImg
     * @param session
     * @return
     */
    @RequestMapping(value = "/modify/avatar", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    String updateAvatar(@RequestParam("avatarImg") String avatarImg, HttpSession session) {
        User user = (User) session.getAttribute(StaticConstUtil.LOGIN_SIGN);
        userService.updateUserAvatar(avatarImg, user.getUid());
        userService.updateSessionUser(session);
        return StaticConstUtil.STATUS_SUCCESS;
    }

    /**
     * 更新学院，专业，班级操作
     *
     * @param college
     * @param marjor
     * @param classes
     * @param session
     * @return
     */
    @RequestMapping(value = "/updateCollegeDepartment", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
    @ResponseBody
    String updateCollege(@RequestParam("college") String college, @RequestParam("major") String marjor, @RequestParam("classes") String classes, HttpSession session) {
        User user = (User) session.getAttribute("user");

        userService.updateCollege(user.getUid(), college, marjor, classes);
        userService.updateSessionUser(session);
        return StaticConstUtil.STATUS_SUCCESS;
    }


    /**
     * 各个学院的列表
     *
     * @return
     */
    @RequestMapping(value = "/collegeList", produces = "text/html;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    String collegeList() {
        List<String> list = userService.collegeList();
        JSONArray jsonArray = new JSONArray().fromObject(list);
        return jsonArray.toString();
    }

    /**
     * 某个专业的列表
     *
     * @param college 学院名称
     * @return
     */
    @RequestMapping(value = "/majorList", produces = "text/html;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    String majorList(@RequestParam("college") String college) {
        List<String> list = userService.majorList(college);
        JSONArray jsonArray = new JSONArray().fromObject(list);
        return jsonArray.toString();
    }

    /**
     * 班级的列表
     *
     * @return
     */
    @RequestMapping(value = "/classesList", produces = "text/html;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    String classesList() {
        List<String> list = userService.classesList();
        JSONArray jsonArray = new JSONArray().fromObject(list);
        return jsonArray.toString();
    }
}
